﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GameControl : System.Web.UI.UserControl
{
    const string PICKEDTEAM = "picked";
    const string NONPICKEDTEAM = "pickButton";
    const string GAMEWON = "gameWon";
    const string GAMELOSS = "gameLoss";
    const string GAMENOPICK = "gameNoPick";
    const string GAMEINPROGRESS = "gameInProgress";
    const string GAMEWINNING = "gameWinning";
    const string GAMELOSING = "gameLosing";
    const string HASPOS = "hasPos";

    UserPick userPick = null;
    Game game;
    User user;
    List<UserPick> userPicks;

    protected int GameID;
    protected int HomeTeamID;
    protected int AwayTeamID;
    protected int UserID;




    public void Init(Game game, List<UserPick> userPicks, User currentUser = null)
    {
        this.game = game;
        this.GameID =(int) game.ID;
        this.HomeTeamID = (int)game.HomeTeam.ID;
        this.AwayTeamID = (int)game.AwayTeam.ID;
        this.userPicks = userPicks;
        this.user = currentUser;

        if (user != null)
        {
            this.UserID = (int)user.ID;
            userPick = (from p in userPicks
                        where p.UserID == user.ID
                        select p).FirstOrDefault();
        }
    }
    public void LoadGame()
    {

        
    
        if (user != null && userPick != null && userPick.ChosenTeamID != null)
        {

            if ( userPick.ChosenTeamID == game.HomeTeam.ID)
            {
                pickHomeTeam.Attributes.Add("class", PICKEDTEAM);
                pickAwayTeam.Attributes.Add("class", NONPICKEDTEAM);
            }
            else
            {
                pickAwayTeam.Attributes.Add("class", PICKEDTEAM);
                pickHomeTeam.Attributes.Add("class", NONPICKEDTEAM);
            }
        }


        if (game != null)
        {
            awayTeamImage.ImageUrl = game.AwayTeam.IconURL;
            awayTeamScore.Text = GetScore(game.AwayScore);
            awayTeamName.Text = game.AwayTeam.TeamName;
            awayTeamRecord.Text = "(" + game.AwayTeamRecord + ")";
            awayTeam.HRef = "DisplayTeamGames.aspx?Team=" + game.AwayTeam.ID.ToString();
            
            homeTeamName.Text = game.HomeTeam.TeamName;
            homeTeamImage.ImageUrl = game.HomeTeam.IconURL;
            homeTeamScore.Text = GetScore(game.HomeScore);
            homeTeamRecord.Text = "(" + game.HomeTeamRecord + ")";
            homeTeam.HRef = "DisplayTeamGames.aspx?Team=" + game.HomeTeam.ID.ToString();


            date.Text = ((DateTime)game.Date).ToString("ddd, MMM d");
            time.Text = ((DateTime)game.Date).ToString("hh:mm tt");

            DetermineGameBoxes();

            ShowSpread();

            if (user != null)
            {
                pickHomeTeam.Attributes.Add("data-user-id", UserID.ToString());
                pickHomeTeam.Attributes.Add("data-team-id", HomeTeamID.ToString());
                pickHomeTeam.Attributes.Add("data-game-id", GameID.ToString());

                pickAwayTeam.Attributes.Add("data-user-id", UserID.ToString());
                pickAwayTeam.Attributes.Add("data-team-id", AwayTeamID.ToString());
                pickAwayTeam.Attributes.Add("data-game-id", GameID.ToString());
            }
            
            if (game.GameState == Game.State.InGame)
            {
                gameControl.Attributes.Add("class", GAMEINPROGRESS);
                gameStatus.Text = "In Game";
                LoadOtherUsersPicks();
                LockGame();
                time.Text = game.GameDetail;
                CheckPossesion();
                if (userPick != null)
                {
                    if (game.AwayScore > game.HomeScore && userPick.ChosenTeamID == game.AwayTeam.ID)
                        gameControl.Attributes.Add("class", GAMEWINNING);
                    else if (game.AwayScore > game.HomeScore && userPick.ChosenTeamID == game.HomeTeam.ID)
                        gameControl.Attributes.Add("class", GAMELOSING);
                    else if (game.AwayScore < game.HomeScore && userPick.ChosenTeamID == game.AwayTeam.ID)
                        gameControl.Attributes.Add("class", GAMELOSING);
                    else if (game.AwayScore < game.HomeScore && userPick.ChosenTeamID == game.HomeTeam.ID)
                        gameControl.Attributes.Add("class", GAMEWINNING);
                }


            }
            else if (game.GameState == Game.State.PreGame)
            {
                gameStatus.Text = "Pre Game";
                HideScoreBoxes();
            }
            else
            {
                gameStatus.Text = "Final";
                LoadOtherUsersPicks();
                LockGame();
                if (userPick != null)
                {
                    if (userPick.ChosenTeamID == game.WinningTeamID)
                        gameControl.Attributes.Add("class", GAMEWON);
                    else
                        gameControl.Attributes.Add("class", GAMELOSS);
                    }
                else if(user != null)
                    gameControl.Attributes.Add("class", GAMENOPICK);
                

            }
             

        }

    }

    private void CheckPossesion()
    {
        if (game.HasPossession == null)
            return;
        else if (game.HasPossession == game.AwayTeam)
        {
            
            awayTeamPOS.Attributes.Add("class", HASPOS);

        }
        else if (game.HasPossession == game.HomeTeam)
        {
            homeTeamPOS.Attributes.Add("class", HASPOS);
        }
    }

    private void ShowSpread()
    {
        if (game.HasSpread)
        {
            divSpread.Visible = true;
            if (userPick != null)
                txtSpread.Text = userPick.Spread.ToString();
            else
                txtSpread.Text = "0";

            if (game.GameState != Game.State.PreGame)
            {
                txtSpread.Enabled = false;
                btnSubmitSpread.Enabled = false;
            }
        }
    }

    private void DetermineGameBoxes()
    {
        string losingScore = "losingScore";
        string winningScore = "winningScore";

        if (game.AwayScore > game.HomeScore)
        { 
            awayTeamScoreBox.Attributes.Add("class", winningScore);
            homeTeamScoreBox.Attributes.Add("class", losingScore);
        }
        else if(game.AwayScore < game.HomeScore)
        {
            awayTeamScoreBox.Attributes.Add("class", losingScore);
            homeTeamScoreBox.Attributes.Add("class", winningScore);
        }
    }

    private void HideScoreBoxes()
    {
        awayTeamScore.Visible = false;
        homeTeamScore.Visible = false;
    }

    private void LoadOtherUsersPicks()
    {
        List<string> homePicksList = new List<string>();
        List<string> awayPicksList = new List<string>();

        awayPicks.Visible = true;
        homePicks.Visible = true;

        foreach (UserPick up in userPicks)  
        {
            if (user == null || up.UserID != user.ID)
            {


                if(up.ChosenTeamID == game.AwayTeam.ID)
                    awayPicksList.Add(up.UserName + (game.HasSpread? "(" + up.Spread + ")":"" ));
                else if (up.ChosenTeamID == game.HomeTeam.ID)
                    homePicksList.Add(up.UserName+ (game.HasSpread? "(" + up.Spread + ")":"" ));
            }
        }

        lstPicksAway.DataSource = awayPicksList.OrderBy(x => x); ;
        lstPicksHome.DataSource = homePicksList.OrderBy(x => x); ;
        lstPicksAway.DataBind();
        lstPicksHome.DataBind();
    }

    private void LockGame()
    {
        //pickAwayTeam.Enabled = false;
        //pickHomeTeam.Enabled = false;
    }

    private string GetScore(int score)
    {
        string returnScore;
        if (score == -1)
            returnScore = "--";
        else
            returnScore = score.ToString();

        return returnScore;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
    
    }

    public void clickHomeTeam(object sender, EventArgs e)
    {
        if (userPick == null)
        {
            userPick = new UserPick()
            {
                ChosenTeamID = game.HomeTeam.ID,
                GameID = (int)game.ID,
                UserID = (int)user.ID
            };

            DataBase.UserPicks.InsertUserPick(userPick);
        }
        else
        {
            if (userPick.ChosenTeamID != game.HomeTeam.ID)
            {
                userPick.ChosenTeamID = game.HomeTeam.ID;
                DataBase.UserPicks.UpdateUserPick(userPick);
            }
        }


    }

    public void clickAwayTeam(object sender, EventArgs e)
    {
        if (userPick == null)
        {
            userPick = new UserPick()
            {
                ChosenTeamID = game.AwayTeam.ID,
                GameID = (int)game.ID,
                UserID = (int)user.ID
            };

            DataBase.UserPicks.InsertUserPick(userPick);
        }
        else
        {
            if (userPick.ChosenTeamID != game.AwayTeam.ID)
            {
                userPick.ChosenTeamID = game.AwayTeam.ID;
                DataBase.UserPicks.UpdateUserPick(userPick);
            }
        }
        


    }
    protected void ClickSubmitSpread(object sender, EventArgs e)
    {
        if (userPick == null)
        {
            userPick = new UserPick()
            {
                ChosenTeamID = null,
                GameID = (int)game.ID,
                UserID = (int)user.ID
            };

            DataBase.UserPicks.InsertUserPick(userPick);
        }
        userPick.Spread = int.Parse(txtSpread.Text);
        DataBase.UserPicks.UpdateUserPick(userPick);
    }
}